package com.sisgesbyte.dao.tecnico;

import java.util.List;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.sisgesbyte.dao.beanDao.FiltrosSQLBean;
import com.sisgesbyte.dao.beanDao.ModuloEditRequest;
import com.sisgesbyte.dao.beanDao.ModuloNuevoRequest;
import com.sisgesbyte.dao.utilDao.CreaFiltroSqlString;
import com.sisgesbyte.data.entity.comun.GenericEntity;
import com.sisgesbyte.data.entity.tecnico.ListaModulosEntity;
import com.sisgesbyte.data.entity.tecnico.ListaTipoModulosEntity;
import com.sisgesbyte.data.util.AbstractFacade;

@Stateless
public class MantenimientoModuloDaoImpl extends
		AbstractFacade<ListaModulosEntity> implements MantenimientoModuloDao {

	@Inject
	private CreaFiltroSqlString creaFiltroSqlString;

	@PersistenceContext(unitName = "SigesSiscompPU")
	private EntityManager em;

	@Override
	protected EntityManager getEntityManager() {
		return em;
	}

	public MantenimientoModuloDaoImpl() {
		super(ListaModulosEntity.class);
	}

	@Override
	public List<ListaModulosEntity> listarModulos(FiltrosSQLBean cadenaFiltros) {

		StringBuilder jpql = new StringBuilder();
		jpql.append("select m.id,m.nombre,m.descripcion,m.summary,"
				+ "m.version,m.icon,m.orden,m.url,"
				+ "m.active,m.tipo,tp.nombre as tipomodulo "
				+ "from tecnico.modulo m "
				+ "inner join tecnico.tipo_modulo tp on m.tipo=tp.id "
				);
		// concatenamos el filtro
		if (cadenaFiltros != null) {
			StringBuilder filtro = new StringBuilder();
			filtro.append(creaFiltroSqlString.concatenar(cadenaFiltros));
			jpql.append(filtro.toString());
		}
		
		Query query = em.createNativeQuery(jpql.toString(), ListaModulosEntity.class);
		
		if (cadenaFiltros != null && cadenaFiltros.getListaValores() != null
				&& cadenaFiltros.getListaValores().size() > 0) {
			int num = 0;
			for (Object valor : cadenaFiltros.getListaValores()) {
				query.setParameter(++num, valor);
			}
		}
		List<ListaModulosEntity> ls = query.getResultList();

		return ls;
	}

	@Override
	public GenericEntity moduloNuevo(ModuloNuevoRequest modulo) {
		StringBuilder jpql = new StringBuilder();
		jpql.append("select * from tecnico.sp_registrar_modulo(:in_nombre, :in_descripcion, :in_summary, :in_version, "
				+ ":in_icon, :in_orden, :in_url, :in_active, :in_tipo )");

		Query query = em
				.createNativeQuery(jpql.toString(), GenericEntity.class);

		query.setParameter("in_nombre", modulo.getNombreModulo());
		query.setParameter("in_descripcion", modulo.getDescripcion());
		query.setParameter("in_summary", modulo.getSummary());
		query.setParameter("in_version", modulo.getVersion());
		query.setParameter("in_icon", modulo.getIcon());
		query.setParameter("in_orden", modulo.getOrden());
		query.setParameter("in_url", modulo.getUrl());
		query.setParameter("in_active", modulo.getActive());
		query.setParameter("in_tipo", modulo.getTipo());

		return (GenericEntity) query.getSingleResult();
	}

	@Override
	public GenericEntity moduloEdit(ModuloEditRequest modulo) {

		StringBuilder jpql = new StringBuilder();

		jpql.append("select * from tecnico.sp_editar_modulo ");
		jpql.append("(:in_codigo , :in_nombre, :in_descripcion, ");
		jpql.append(":in_summary , :in_version, :in_icon, ");
		jpql.append(":in_orden , :in_url, :in_active, :in_tipo )");

		Query query = em
				.createNativeQuery(jpql.toString(), GenericEntity.class);
		query.setParameter("in_codigo", modulo.getCodModulo());
		query.setParameter("in_nombre", modulo.getNombreModulo());
		query.setParameter("in_descripcion", modulo.getDescripcion());
		query.setParameter("in_summary", modulo.getSummary());
		query.setParameter("in_version", modulo.getVersion());
		query.setParameter("in_icon", modulo.getIcon());
		query.setParameter("in_orden", modulo.getOrden());
		query.setParameter("in_url", modulo.getUrl());
		query.setParameter("in_active", modulo.getActive());
		query.setParameter("in_tipo", modulo.getTipo());

		return (GenericEntity) query.getSingleResult();
	}

	@Override
	public GenericEntity moduloDelete(int codModulo) {

		StringBuilder jpql = new StringBuilder();

		jpql.append("select * from tecnico.sp_eliminar_modulo (:in_codigo)");

		Query query = em
				.createNativeQuery(jpql.toString(), GenericEntity.class);
		query.setParameter("in_codigo", codModulo);

		return (GenericEntity) query.getSingleResult();
	}

	@Override
	public List<ListaTipoModulosEntity> listaTipoModulos() {

		StringBuilder jpql = new StringBuilder();
		jpql.append("select tm.id, tm.nombre from tecnico.tipo_modulo tm");

		Query query = em.createNativeQuery(jpql.toString(),
				ListaTipoModulosEntity.class);
		List<ListaTipoModulosEntity> ls = query.getResultList();

		return ls;
	}

}
